﻿using BaseUi.Device;
using BaseUi.Models;
using BaseUi.ViewModel;
using BaseUi.ViewModels;
using Ping9719.DeviceCommunication;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi
{
    /// <summary>
    /// VisiionChangeView.xaml 的交互逻辑
    /// </summary>
    public partial class VisiionChangeView : UserControlBase
    {
        VisiionChangeViewModel viewModel = new VisiionChangeViewModel();
        /// <summary>
        /// 是否点击准备
        /// </summary>
        bool iszhunbei = false;
        int direction;//（1是12在外面）
        public VisiionChangeView()
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }
        /// <summary>
        ///  是否可以旋转
        /// </summary>
        /// <returns></returns>
        private string sfsafe()
        {
            if (!MainWindow2.viewModel.ischengg)
            {
                return MainWindow2.viewModel.errtext;
            }
            else
            {

                //}
                //var ioi = LocalStatic.Electric.Readsentfrequency(500, PortModel.EAll, LocalFile.Config.Address);
                //if (!ioi.IsSucceed)
                //{
                //    return ioi.Err;
                //}
                //else
                //{
                string[] rest = MainWindow2.viewModel.msg;
                //string[] rest = ioi.Value.Split(' ');

                //var send = SetTrigger( LocalFile.Config.Address);
                //if (ioi.Value.Length != 42)
                //{
                //    return "长度错误";
                //}
                if (rest[2] != "01")
                {
                    return "顶升1-1顶断开未检测到位" + rest[2];
                }

                if (rest[3] != "00")
                {
                    return "顶升1-2顶闭合异常" + rest[3];
                }
                if (rest[4] != "01")
                {
                    return "顶升2-1顶断开未检测到位" + rest[4];
                }
                if (rest[5] != "00")
                {
                    return "顶升2-2顶闭合异常" + rest[5];
                }

                if (rest[6] != "01")
                {
                    //return "开关箱报警";
                }
                if (rest[8] != "00")
                {
                    return "急停按钮报警";
                }
                if (rest[9] != "00")
                {
                    //return "光栅报警";
                }
                if (rest[10] != "01")
                {
                    return "右按压气缸未上升";
                }
                if (rest[11] != "00")
                {
                    return "右按压气缸下降";
                }
                if (rest[12] != "01")
                {
                    return "左按压气缸未上升";
                }
                if (rest[13] != "00")
                {
                    return "左按压气缸下降";
                }
            }
            return "OK";
        }
        /// <summary>
        /// 转盘是否到位
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        //private string reach()
        //{
        //    string retul;
        //    var de = LocalStatic.Electric.Readsentfrequency(500, PortModel.Accurate, LocalFile.Config.Address);

        //    if (de.IsSucceed)
        //    {
        //        if (de.Value == "00 01 ")
        //        {
        //            direction = 2;
        //            return"ok";
        //        }
        //        else if (de.Value == "01 00 ")
        //        {

        //            direction = 1;
        //            return "ok";
        //        }
        //        else if (de.Value == "00 00 ")
        //        {
        //            direction = 0;
        //            return "转盘未旋转到位";
        //        }
        //        else
        //        {
        //            direction = 3;
        //            return "转盘正反都有到位信号";
        //        }
        //    }
        //    else
        //    {
        //        return "转盘到位检测失败";
        //    }

        //}
        /// <summary>
        /// 按压气缸与顶升气缸是否可以使用
        /// </summary>
        /// <returns></returns>
        private string anysafe()
        {
            if (!MainWindow2.viewModel.ischengg)
            {
                return "转盘状态通信失败，" + MainWindow2.viewModel.errtext;
            }
            string dian = MainWindow2.viewModel.msg[0] + " " + MainWindow2.viewModel.msg[1] + " ";
            if (dian == "00 01 ")
            {
                direction = 2;
                return "OK";
            }
            else if (dian == "01 00 ")
            {
                direction = 1;
                return "OK";
            }
            else if (dian == "00 00 ")
            {
                direction = 0;
                return "转盘状态异常,未到位";
            }
            else
            {
                direction = 3;
                return "转盘状态异常。两边同时到位";
            }
        }
        private void loadstart(object sender, EventArgs e)
        {
            img1.StartListener(LocalFile.Config.img1);
            img2.StartListener(LocalFile.Config.img2);
            //anysafe();
            //for (int vnm = 0; vnm < 4; vnm++)
            //{
            //    string retul = "";
            //    var s1 = LocalStatic.Test[vnm].ReadOn();
            //    if (!s1.IsSucceed)
            //    {
            //        textBoxInfo.AppendText((vnm + 1) + "号测试位闭合继电器失败");
            //    }
            //    else
            //    {
            //        s1 = LocalStatic.Test[vnm].ReadCurrent();
            //        retul = getvision1("进入模式", vnm);
            //        if (retul != "OK")
            //        {
            //            textBoxInfo.AppendText((vnm + 1) + "号测试位进入页面切换模式失败");
            //        }
            //    }
            //}

        }

        /// <summary>
        ///反向旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xz1(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = sfsafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate1, PortModel.Rotate2, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"反向旋转成功\r\n");
                    HomeView.viewModel.way = 0;
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        /// <summary>
        ///正向旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xz2(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = sfsafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate2, PortModel.Rotate1, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"正向旋转成功\r\n");
                    HomeView.viewModel.way = 0;
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将气缸顶升
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ds(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = anysafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Jack, new List<int>(), LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"顶升气缸成功顶升\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将气缸顶升断电
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dsdk(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = anysafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Jack, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"顶升气缸成功下降\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        private void ay(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = anysafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Suppress, new List<int>(), LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"按压气缸成功按压\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        /// <summary>
        /// 将按压气缸按压断电
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aydk(object sender, RoutedEventArgs e)
        {
            try
            {
                string retul = anysafe();
                if (retul != "OK")
                {
                    textBoxInfo.AppendText($"{retul}\r\n");
                    return;
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Suppress, LocalFile.Config.Address);
                if (aaa.IsSucceed)
                {
                    textBoxInfo.AppendText($"按压气缸成功断开\r\n");
                }
                else
                {
                    textBoxInfo.AppendText($"通信失败：{aaa.Err}\r\n");
                }
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void Lvisiion(object sender, RoutedEventArgs e)
        {
            string retul = anysafe();
            if (retul != "OK")
            {
                textBoxInfo.AppendText($"{retul}\r\n");
                return;
            }
            int vnm;//测试位
            if (direction == 2)
            {
                vnm = 1;
            }
            else
            {
                vnm = 3;
            }
            try
            {
                string engname = "";
                retul = "";
                var aaa = new Result<string>();
                switch ((sender as RadioButton).Name)
                {

                    case "Avision0":
                        retul = getvision1("进入模式", vnm);
                        engname = "进入模式";
                        break;
                    case "Avision1":
                        retul = getvision1("页面1", vnm);
                        engname = "页面1";
                        break;
                    case "Avision2":
                        retul = getvision1("页面2", vnm);
                        engname = "页面2";
                        break;
                    case "Avision3":
                        retul = getvision1("页面3", vnm);
                        engname = "页面3";
                        break;
                    case "Avision4":
                        retul = getvision1("页面4", vnm);
                        engname = "页面4";
                        break;
                    case "Avision5":
                        retul = getvision1("页面5", vnm);
                        engname = "页面5";
                        break;
                    case "Avision6":
                        retul = getvision1("页面6", vnm);
                        engname = "页面6";
                        break;
                    case "Avision7":
                        retul = getvision1("页面7", vnm);
                        engname = "页面7";
                        break;
                }
                if (retul == "OK")
                {
                    Lget.Text = (vnm + 1) + "测试位视觉合格";
                    LtextBoxInfo.AppendText($"{engname}视觉测试成功\r\n");
                }
                else
                {
                    Lget.Text = (vnm + 1) + "测试位视觉失败";
                    LtextBoxInfo.AppendText($"切换页面失败{retul}\r\n");
                }
            }
            catch (Exception ex)
            {
                LtextBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }
        private string getvision1(string msg, int v)
        {
            var result = new Result<string>();//切换页面返回结果
            string getsen = "";//视觉返回结果
            string fangxiang = "";//方向左或右
            int ampip = 0;//安普板IP
            ChangeBan qhaun = LocalStatic.Change1;//切换页面的串口对象
            VisionBan Vision = LocalStatic.Vision2;//视觉对象
            if (v == 0)
            {
                qhaun = LocalStatic.Change1;
                Vision = LocalStatic.Vision2;
                ampip = LocalFile.Config.Amp1;
                fangxiang = "R";
            }
            else if (v == 1)
            {
                qhaun = LocalStatic.Change1;
                Vision = LocalStatic.Vision1;
                ampip = LocalFile.Config.Amp2;
                fangxiang = "L";
            }
            else if (v == 2)
            {
                qhaun = LocalStatic.Change2;
                Vision = LocalStatic.Vision2;
                ampip = LocalFile.Config.Amp3;
                fangxiang = "R";
            }
            else if (v == 3)
            {
                qhaun = LocalStatic.Change2;
                Vision = LocalStatic.Vision1;
                ampip = LocalFile.Config.Amp4;
                fangxiang = "L";
            }
            switch (msg)
            {
                case "进入模式":
                    result = qhaun.Readsent1(500, ampip);
                    if (!result.IsSucceed)
                    {
                        Thread.Sleep(50);
                    }
                    result = qhaun.Readsent1(500, ampip);
                    if (!result.IsSucceed)
                    {
                        return "进入切换页面模式失败" + result.Err;
                    }
                    break;
                case "页面1":
                    result = qhaun.Readsent3(500, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面1失败" + result.Err;
                    }
                    getsen = Vision.SendMes(fangxiang + PortModel.Vision1);
                    if (getsen != fangxiang + "CH1;OK")
                    {
                        return "页面1视觉失败" + getsen;
                    }
                    break;
                case "页面2":
                    result = qhaun.Readsent4(2000, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面2失败" + result.Err;
                    }
                    getsen = Vision.SendMes(fangxiang + PortModel.Vision2);
                    if (getsen != fangxiang + "CH2;OK")
                    {
                        return "页面2视觉失败" + getsen;
                    }
                    break;
                case "页面3":
                    result = qhaun.Readsent5(2000, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面3失败" + result.Err;
                    }
                    getsen = Vision.SendMes(fangxiang + PortModel.Vision3);
                    if (getsen != fangxiang + "CH3;OK")
                    {
                        return "页面3视觉失败" + getsen;
                    }
                    break;
                case "页面4":
                    result = qhaun.Readsent6(2000, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面4失败" + result.Err;
                    }
                    getsen = Vision.SendMes(fangxiang + PortModel.Vision4);
                    if (getsen != fangxiang + "CH4;OK")
                    {
                        return "页面4视觉失败" + getsen;
                    }
                    break;
                case "页面5":
                    result = qhaun.Readsent7(2000, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面5失败" + result.Err;
                    }
                    getsen = Vision.SendMes(fangxiang + PortModel.Vision5);
                    if (getsen != fangxiang + "CH5;OK")
                    {
                        return "页面5视觉失败" + getsen;
                    }
                    break;
                case "页面6":
                    result = qhaun.Readsent8(2000, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面6失败" + result.Err;
                    }
                    getsen = Vision.SendMes(fangxiang + PortModel.Vision6);
                    if (getsen != fangxiang + "CH6;OK")
                    {
                        return "页面6视觉失败" + getsen;
                    }
                    break;
                case "页面7":
                    result = qhaun.Readsent9(2000, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面7失败" + result.Err;
                    }
                    getsen = Vision.SendMes(fangxiang + PortModel.Vision7);
                    if (getsen != fangxiang + "CH7;OK")
                    {
                        return "页面7视觉失败" + getsen;
                    }
                    break;
            }
            return "OK";
        }

        private void Rvisiion(object sender, RoutedEventArgs e)
        {
            string retul = anysafe();
            if (retul != "OK")
            {
                textBoxInfo.AppendText($"{retul}\r\n");
                return;
            }
            int vnm;//测试位
            if (direction == 2)
            {
                vnm = 0;
            }
            else
            {
                vnm = 2;
            }
            try
            {
                string engname = "";
                retul = "";
                var aaa = new Result<string>();
                switch ((sender as RadioButton).Name)
                {

                    case "Bvision0":
                        retul = getvision1("进入模式", vnm);
                        engname = "进入模式";
                        break;
                    case "Bvision1":
                        retul = getvision1("页面1", vnm);
                        engname = "页面1";
                        break;
                    case "Bvision2":
                        retul = getvision1("页面2", vnm);
                        engname = "页面2";
                        break;
                    case "Bvision3":
                        retul = getvision1("页面3", vnm);
                        engname = "页面3";
                        break;
                    case "Bvision4":
                        retul = getvision1("页面4", vnm);
                        engname = "页面4";
                        break;
                    case "Bvision5":
                        retul = getvision1("页面5", vnm);
                        engname = "页面5";
                        break;
                    case "Bvision6":
                        retul = getvision1("页面6", vnm);
                        engname = "页面6";
                        break;
                    case "Bvision7":
                        retul = getvision1("页面7", vnm);
                        engname = "页面7";
                        break;
                }
                if (retul == "OK")
                {
                    Rget.Text = (vnm + 1) + "测试位视觉合格";
                    RtextBoxInfo.AppendText($"{engname}视觉测试成功\r\n");
                }
                else
                {
                    Rget.Text = (vnm + 1) + "测试位视觉失败";
                    RtextBoxInfo.AppendText($"切换页面失败{retul}\r\n");
                }
            }
            catch (Exception ex)
            {
                RtextBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void loadend(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                if (iszhunbei)
                {
                    iszhunbei = false;
                    for (int vnm = 0; vnm < 4; vnm++)
                    {
                        var s1 = LocalStatic.Test[vnm].Readoff();
                        if (!s1.IsSucceed)
                        {
                            s1 = LocalStatic.Test[vnm].Readoff();
                        }
                    }
                }
            });
        }

        private void loadstart(object sender, RoutedEventArgs e)
        {
            if (this.IsInDesignMode)
                return;
            img1.StartListener(LocalFile.Config.img1);
            img2.StartListener(LocalFile.Config.img2);

        }

        private void textchange(object sender, TextChangedEventArgs e)
        {
            var nam = (sender as TextBox);
            nam.ScrollToEnd();
        }

        private void clear1(object sender, RoutedEventArgs e)
        {
            textBoxInfo.Clear();
        }

        private void clear2(object sender, RoutedEventArgs e)
        {
            LtextBoxInfo.Clear();
        }
        private void clear3(object sender, RoutedEventArgs e)
        {
            RtextBoxInfo.Clear();
        }
        
        private void shijuezb(object sender, RoutedEventArgs e)
        {
            iszhunbei = true;
            Task.Run(() =>
            {
                string retul = "";
                //for (int vnm = 0; vnm < 4; vnm++)
                //{

                //    var s1 = LocalStatic.Test[vnm].ReadOn();
                //    if (!s1.IsSucceed)
                //    {
                //        s1 = LocalStatic.Test[vnm].ReadOn();
                //        if (!s1.IsSucceed)
                //        {
                //            retul = retul + (vnm + 1) + "号测试位视觉准备失败\r\n";
                //        }
                //        else
                //        {
                //            viewModel.textbox = viewModel.textbox + ($"{vnm + 1}号测试位视觉准备成功\r\n");
                //        }
                //    }
                //    else
                //    {
                //        viewModel.textbox = viewModel.textbox + ($"{vnm + 1}号测试位视觉准备成功\r\n");
                //    }
                //}
                //Thread.Sleep(50);
                for (int vnm = 0; vnm < 4; vnm++)
                {

                    var s1 = LocalStatic.Test[vnm].ReadCurrent();
                    if (!s1.IsSucceed)
                    {
                        s1 = LocalStatic.Test[vnm].ReadCurrent();
                        if (!s1.IsSucceed)
                        {
                            retul = retul + (vnm + 1) + "号测试位失败供电失败\r\n";
                        }
                        else
                        {
                            viewModel.textbox = viewModel.textbox + ($"{vnm + 1}号测试位供电成功\r\n");
                        }
                    }
                    else
                    {
                        viewModel.textbox = viewModel.textbox + ($"{vnm + 1}号测试位供电成功\r\n");
                    }
                }
                for (int vnm = 0; vnm < 4; vnm++)
                {

                    var s1 = LocalStatic.Test[vnm].ReadOn();
                    if (!s1.IsSucceed)
                    {
                        s1 = LocalStatic.Test[vnm].ReadOn();
                        if (!s1.IsSucceed)
                        {
                            retul = retul + (vnm + 1) + "号测试位视觉准备失败\r\n";
                        }
                        else
                        {
                            viewModel.textbox = viewModel.textbox + ($"{vnm + 1}号测试位视觉准备成功\r\n");
                        }
                    }
                    else
                    {
                        viewModel.textbox = viewModel.textbox + ($"{vnm + 1}号测试位视觉准备成功\r\n");
                    }
                }
                Thread.Sleep(50);
                if (retul=="")
                {
                    viewModel.textbox = viewModel.textbox + ($"视觉准备完成\r\n");
                }

            });
            
        }
    }
}
